package hunterxhunter.greedisland.entities.menu;

import hunterxhunter.greedisland.GreedIslandGame;
import hunterxhunter.greedisland.data.Card;
import hunterxhunter.greedisland.data.CardManager;
import hunterxhunter.greedisland.data.Ring;
import hunterxhunter.greedisland.data.Slot;
import hunterxhunter.greedisland.events.KeyPressedUseActionEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import framework.eea.StateBasedGame;
import framework.eea.entity.Entity;
import framework.eea.event.Component;
import framework.eea.event.Event;
import framework.eea.gui.Graphics;
import framework.eea.gui.JFrameGameContainer;
import framework.eea.interfaces.Action;

/**
 *
 * @author Markus Schr&ouml;der
 */
public class BookEntity extends Entity {

    private Rectangle rect;
    private int minCardNum;
    private int selectedSlotNum;
    private int selectionX = 0;
    private int selectionY = 0;
    private int[][] currentPageMap;

    public BookEntity(String id) {
        super(id);
        //setSize(new Dimension(380, 230));
        setSize(new Dimension(300, 190));

        rect = new Rectangle(5, GreedIslandGame.getScreenSize().height - getSize().height - 5, getSize().width, getSize().height);

        minCardNum = 10;
        selectedSlotNum = minCardNum;

        currentPageMap = getPageMap(minCardNum);
        selectedSlotNum = currentPageMap[selectionX][selectionY];

        Event pageEvent = new KeyPressedUseActionEvent(
                new int[]{KeyEvent.VK_PAGE_DOWN, KeyEvent.VK_PAGE_UP}, new Action[]{
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    prevPage();
                }
            },
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    nextPage();
                }
            },});

        Event cardEvent = new KeyPressedUseActionEvent(
                new int[]{KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT, KeyEvent.VK_UP, KeyEvent.VK_DOWN,}, new Action[]{
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {

                    //COVER
                    if (minCardNum == -26) {
                        return;
                    }

                    //BACKSIDE
                    if (minCardNum == 154) {
                        prevPage();
                        return;
                    }

                    selectionX--;

                    if (selectionX < 3 && selectedSlotNum == 0) {
                        prevPage();
                        selectionX++;
                    } else if (selectionX < 0) {
                        selectionX = 5;
                        prevPage();
                    }
                    selectedSlotNum = currentPageMap[selectionX][selectionY];
                }
            },
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    //COVER
                    if (minCardNum == -26) {
                        nextPage();
                        return;
                    }

                    //BACKSIDE
                    if (minCardNum == 154) {
                        return;
                    }

                    selectionX++;
                    if (selectionX > 2 && minCardNum == 136) {
                        nextPage();
                        selectionX--;
                    } else if (selectionX > 5) {
                        selectionX = 0;
                        nextPage();
                    }
                    selectedSlotNum = currentPageMap[selectionX][selectionY];
                }
            },
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    selectionY--;
                    if (selectionY < 0) {
                        selectionY = 2;
                    }
                    selectedSlotNum = currentPageMap[selectionX][selectionY];
                }
            },
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    selectionY++;
                    if (selectionY > 2) {
                        selectionY = 0;
                    }
                    selectedSlotNum = currentPageMap[selectionX][selectionY];
                }
            },});

        
        //HACK
        Event getFixedCardEvent = new KeyPressedUseActionEvent(
                new int[]{KeyEvent.VK_C}, new Action[]{
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    if (selectedSlotNum >= 0 && selectedSlotNum <= 99) {
                        Ring ring = ((GreedIslandGame) sb).getRing();
                        //TODO
                        Slot s = ring.getSlot(selectedSlotNum);

                        if (s.isFixed()) {
                            s.collectCard(selectedSlotNum);
                        }

                        //ring.getImposedSlots()[selectedCardNum] = !ring.getImposedSlots()[selectedCardNum];
                    }
                }
            },});
        
        Event getAllSpellCardEvent = new KeyPressedUseActionEvent(
                new int[]{KeyEvent.VK_A}, new Action[]{
            new Action() {
                public void update(JFrameGameContainer gc, StateBasedGame sb, int delta, Component event) {
                    Ring ring = ((GreedIslandGame) sb).getRing();
                    ring.getAllSpellCards();
                }
            },});
        
        

        addComponent(pageEvent);
        addComponent(cardEvent);
        
        //HACK
        addComponent(getFixedCardEvent);
        addComponent(getAllSpellCardEvent);
    }

    private void prevPage() {
        if (minCardNum != -26) {
            minCardNum -= 18;
            currentPageMap = getPageMap(minCardNum);
            selectedSlotNum = currentPageMap[selectionX][selectionY];
        }
    }

    private void nextPage() {
        if (minCardNum != 154) {
            minCardNum += 18;
            currentPageMap = getPageMap(minCardNum);
            selectedSlotNum = currentPageMap[selectionX][selectionY];
        }
    }

    private int[][] getPageMap(int minCardNum) {

        int[][] map = new int[6][3];

        int curCardNum = minCardNum;

        for (int page = 0; page < 2; page++) {
            for (int i = 0; i < 9; i++) {

                int x = i % 3;
                int y = i / 3;

                map[page * 3 + x][y] = curCardNum < 0 ? 0 : curCardNum;

                curCardNum++;
            }
        }

        return map;
    }

    @Override
    public void render(JFrameGameContainer gc, StateBasedGame sb, Graphics g) {
        if (isVisible()) {

            Ring ring = ((GreedIslandGame) sb).getRing();

            //exception
            if (minCardNum == -26) {
                g.drawImage("/greedisland/img/book-front.jpg", new Rectangle(rect.x + rect.width / 2, rect.y, rect.width / 2, rect.height));
                return;
            }
            if (minCardNum == 154) {
                g.drawImage("/greedisland/img/book-back.jpg", new Rectangle(rect.x, rect.y, rect.width / 2, rect.height));
                return;
            }

            g.setColor(Color.lightGray);
            g.fillRect(rect);

            g.setColor(Color.black);
            g.drawRect(rect);
            g.drawLine(rect.x + rect.width / 2, rect.y, rect.x + rect.width / 2, rect.y + rect.height);

            int pageW = rect.width / 2;
            int pageH = rect.height;

            int cardW = pageW / 3;
            int cardH = pageH / 3;

            int curSlotNum = minCardNum;

            for (int page = 0; page < 2; page++) {
                for (int i = 0; i < 9; i++) {

                    if (curSlotNum < 0 || curSlotNum > 144) {
                        curSlotNum++;
                        continue;
                    }

                    int x = i % 3;
                    int y = i / 3;

                    Rectangle card = new Rectangle(rect.x + pageW * page + cardW * x, rect.y + cardH * y, cardW, cardH);

                    //exception
                    if (curSlotNum == 0) {
                        card.x -= card.width;
                        card.y -= card.height;
                    }

                    int boarderX = 7;
                    int boarderY = 7;
                    card.x += boarderX;
                    card.y += boarderY;
                    card.width -= boarderX * 2;
                    card.height -= boarderY * 2;

                    int centerX = card.x + card.width / 2;
                    int centerY = card.y + card.height / 2;

                    if (curSlotNum == selectedSlotNum) {
                        g.setColor(Color.red);
                    } else {
                        g.setColor(Color.black);
                    }

                    //rects
                    Rectangle smallCardRect = new Rectangle(card);
                    smallCardRect.grow(-2, -2);
                    Rectangle imageCardRect = new Rectangle(smallCardRect.x + 2, smallCardRect.y + 7, 28, 18);
                    g.drawRect(card);

                        
                    //only if you have it in the ring
                    Slot curSlot = ring.getSlot(curSlotNum);
                    if (curSlot.hasCard()) {
                        //small card
                        Card currentCard = CardManager.getInstance().getCard(curSlot.getCardNumber());
                        if (currentCard != null) {
                            //background
                            g.drawImage("/greedisland/img/card.png", smallCardRect);
                            //render small image
                            g.drawImage(currentCard.getImageRef(), imageCardRect);
                        }
                    }
                    
                    //number only for fixed
                    if(curSlot.isFixed())
                        g.drawStringCenter(String.format("%03d", curSlotNum), centerX, centerY + 10);
                
                    curSlotNum++;
                    
                }//for i 0 - 8
            }//for page 0-1
            
            //int cardX = 500 - 192;
            //int cardY = 500 - 265;
            
            //show selected card
            Slot selectedSlot = ring.getSlot(selectedSlotNum);
            if (selectedSlot.hasCard()) {
                //big card
                Card selectedCard = CardManager.getInstance().getCard(selectedSlot.getCardNumber());
                if (selectedCard != null) {
                    //render big one
                    selectedCard.render(310, 250, g);
                }
            }
            
        }//is Visible
    }
}
